const co = require("co");
const {
  fetch
} = require("tiny-fetch")
const config = require('./../../config');
const payAppUrl = config.app.app;
const appUrl = config.app.appUrl;
const cdcUrl = config.app.cdcUrl;
const notifyHost = config.app.notifyHost;
const uuid = require('node-uuid');

exports.cancelOrderToFbs = function(orderId) {
  return co(function*() {
    const result = yield fetch(cdcUrl + "/order/cancelOrder", {
      method: 'PUT',
      body: {
        orderNo: orderId
      },
      headers: {
        token: "5429ca08df50392c974c5e71b8db12ab"
      }
    });
    return result.data;
  }).catch(function(e) {
    throw e
  })
}

exports.refundOrderToFbs = function(data) {
  return co(function*() {
    const result = yield fetch(cdcUrl +
      '/order/refundOrder', {
        method: 'POST',
        body: data,
        headers: {
          token: "5429ca08df50392c974c5e71b8db12ab"
        }
      });
    return result.data;
  }).catch(function(e) {
    throw e
  })
}

exports.refundOrderToApp = function(orderId) {
  return co(function*() {

  })
}

exports.getOrderFromApp = function(order) {
  return co(function*() {
    let data = {};
    data = {
      storeId: order.storeId,
      orderId: order.id,
      wechatOrderId: ""
    }
    const payOrder = yield fetch(
      config.app.app + "/api/wechatpay/queryPay", {
        method: 'POST',
        body: data
      })
    if (payOrder.data.scanPayResData.result_code == "SUCCESS" &&
      payOrder.data.scanPayResData.trade_state == "SUCCESS") {
      return payOrder.data;
    }
    return payOrder.data;
  }).catch(function(e) {
    throw e
  })
}

exports.getOrderFromFbs = function(orderId) {
  return co(function*() {
    let result = yield fetch(cdcUrl + '/order/getOrderById/' + orderId);
    return result.data;
  }).catch(function(e) {
    throw e
  })
}

exports.refundOrderFromApp = function(order) {
  return co(function*() {
    let data = {};
    order.amountDue = order.subtotal;
    data = {
      storeId: order.storeId,
      payAmount: 100 * order.amountDue,
      refundFee: 100 * order.amountDue,
      orderId: order.id
    }
    let result = yield fetch(payAppUrl + "/api/wechatpay/refundOrder", {
      method: 'POST',
      body: data
    });
    return result.data;
  }).catch(function(e) {
    throw e
  })
}

exports.notifyOrderToCns = function(data) {
  return co(function*() {
    const result = yield fetch(
      notifyHost + "/notify", {
        method: 'POST',
        body: data
      })
    return result.data;
  }).catch(function(e) {
    throw e
  })
}


exports.completeOrderToFbs = function(data) {
  return co(function*() {
    let result = yield fetch(
      cdcUrl + '/order/completePay', {
        method: 'PUT',
        body: data,
        headers: {
          token: "5429ca08df50392c974c5e71b8db12ab"
        }
      })
  }).catch(function(e) {
    throw e
  })
}

exports.getOrderFromWechat = function(orderId) {
  return co(function*() {

  }).catch(function(e) {
    throw e
  })
}
